package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class CustomAdapterEx4 extends Activity {
	ArrayList<MyFruit> arList;
	MyFruit mf;
	FruitAdapter adapter;
	ListView list;
	int[] img = { R.drawable.good, R.drawable.fig };
	String[] fruit = { "Apple", "Apricot", "Avocado", "Banana", "Blackberry",
			"Blueberry", "Cherry", "Coconut", "Cranberry", "Grape Raisin",
			"Honeydew", "Jackfruit", "Lemon", "Lime", "Mango", "Watermelon" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.customadapterex);

		arList = new ArrayList<MyFruit>();

		for (int i = 0; i < fruit.length; i++) {
			if (i == 0) {
				mf = new MyFruit(img[0], fruit[i]);
			} else if (i == 3) {
				mf = new MyFruit(img[0], fruit[i]);
			} else if (i == 6) {
				mf = new MyFruit(img[0], fruit[i]);
			} else {
				mf = new MyFruit(img[1], fruit[i]);
			}
			arList.add(mf);
		}

		adapter = new FruitAdapter(this, arList);
		
		list = (ListView) findViewById(R.id.customeadapterexlistview);
		list.setAdapter(adapter);
		list.setOnItemClickListener(mClickListener);

	}
	
	OnItemClickListener mClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Toast.makeText(CustomAdapterEx4.this, "이 과일은 " + arList.get(position).getFruitNum() , Toast.LENGTH_SHORT).show();
		}
		
	};
}

class MyFruit {
	int Icon;
	String fruitNum;

	public MyFruit(int icon, String fruitNum) {
		super();
		Icon = icon;
		this.fruitNum = fruitNum;
	}

	public int getIcon() {
		return Icon;
	}

	public String getFruitNum() {
		return fruitNum;
	}

	public void setIcon(int icon) {
		Icon = icon;
	}

	public void setFruitNum(String fruitNum) {
		this.fruitNum = fruitNum;
	}

}

class FruitAdapter extends BaseAdapter {
	ArrayList<MyFruit> arFruit;
	LayoutInflater inflater;
	Context mContext;
	
	public FruitAdapter(Context context, ArrayList<MyFruit> arList) {
		mContext = context;
		arFruit = arList;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);		
	}

	@Override
	public int getCount() {
		return arFruit.size();
	}

	@Override
	public Object getItem(int position) {
		return arFruit.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			convertView = inflater.inflate(R.layout.customadapterex3contents, parent, false);
		}
		
		ImageView icon = (ImageView) convertView.findViewById(R.id.fruitlistiv);
		icon.setImageResource(arFruit.get(position).getIcon());
		
		TextView fName = (TextView) convertView.findViewById(R.id.fruitlisttv);
		fName.setText(arFruit.get(position).getFruitNum());
		
		return convertView;
	}
}
